window.onload = function () {
  //计时器开始
  function daojishi (time) {
    var h = parseInt(time / 60 / 60 % 24);
    var m = parseInt(time / 60 % 60);
    var s = parseInt(time % 60);
    return h + ':' + m + ':' + s;
  }
  var time = 1800;
  function resetTime () {
    var timer = document.querySelector('.timer');
    timer.innerHTML = daojishi(time);
    time--;
  }
  // window.setInterval(resetTime, 1000);
  //计时器结束

  var flag = 0;
  var qipan = document.querySelectorAll('.in li');
  var huiqi = [];
  var white = []; //存放白棋子的坐标
  var black = []; //存放黑棋子的坐标
  
  
  //给每个旗格添加属性 用于标记位置-----开始
  for (var i = 0; i < qipan.length; i++){
    qipan[i].setAttribute('data-index', i);
  }
  //给每个旗格添加属性 -----结束


  //下棋 开始
  
  var whiteResult = [];
  var blackResult = [];
  var cont = 0;//记录点击次数
  for (var i = 0; i < qipan.length; i++){
    qipan[i].addEventListener('click', function (e) {
      var qizi = e.target;
      var temp = parseInt(e.target.getAttribute('data-index'));
      if (flag == 0) {
        e.target.innerHTML +='<img src="白.png">';
        flag = 1;
        white[cont/2] = temp;
        whiteResult = white;
        sortArray(whiteResult);
      } else {
        e.target.innerHTML +="<img src='黑.png'>";
        flag = 0;
        // regret.addEventListener('click', regret2);
        black[parseInt(cont / 2)] = temp;
        blackResult = black;
        sortArray(blackResult);
      }
      if (getWinResult(whiteResult)) {
        alert('游戏结束,白棋获胜！！');
        window.clearInterval(timerr);
      }
      if (getWinResult(blackResult)) {
        alert('游戏结束，黑棋获胜！！');
        window.clearInterval(timerr);
      }
      cont++;
    })
  }
  //下棋结束
  
  //验证输赢-----开始
  function getWinResult (white) {
    var cont = 1;
    for (var i = 1; i < white.length; i++) {
      if (white[i] == white[i - 1] + 1) {
        cont++;
      } else {
        cont = 1;
      }
      if (cont == 5) {
        return true;
      }
    }
    cont = 1;
    for (var i = 1; i < white.length; i++) {
      if (white[i] == white[i - 1] + 16 && parseInt(white[i - 1] / 16) + 1 <= 9) {
        cont++;
      } else {
        cont = 1;
      }
      if (cont == 5) {
        return true;
      }
    }
    cont = 1; 
    for (var i = 0; i < white.length; i++) {
      var temp = white[0];
      for (j = i; j < white.length; j++) {
        if (white[j] == temp + 17) {
          temp = white[j];
          cont++;
        }
      }
      if (cont == 5) return true;
      cont = 1;
    }
    for (var i = 0; i < white.length; i++) {
      var temp = white[0];
      for (j = i; j < white.length; j++) {
        if (white[j] == temp + 15) {
          temp = white[j];
          cont++;
        }
      }
      if (cont == 5) return true;
      cont = 1;
    }
    return false;
  }  
  function sortArray (white) {
    var temp;
    for (var i = 0; i < white.length; i++){
      for (var j = 0; j < white.length-i-1; j++){
        if (white[j] > white[j + 1]) {
          temp = white[j];
          white[j] = white[j + 1];
          white[j + 1] = temp;
        }
      }
    }
  }
  //验证输赢-----结束

  //开始游戏
  var timerr;
  var start = document.querySelector('.start');
  var cover = document.querySelector('.cover');
  start.addEventListener('click', function () {
    // location.href = "file:///F:/Web%20develop/%E4%BA%94%E5%AD%90%E6%A3%8B/main.html";
    timerr = window.setInterval(resetTime, 1000);
    cover.style.zIndex = -10;
  })
  
  //开始游戏


  //悔棋功能-----开始
  function regretqi () {
    if (flag == 1) {
      flag = 0;
      var lengthh = white.length - 1;
      // console.log(qipan[white[lengthh]]);
      qipan[white [lengthh]].innerHTML = "";
      cont--;
      white.length = parseInt(cont / 2);
    }
    else {
      flag = 1;
      // qipan[black[black.length]].innerHTML += "ss";
      var lengthh = black.length - 1;
      qipan[black[lengthh]].innerHTML = "";
      cont--;
      black.length = parseInt(cont / 2);
    }
  }
  var ret = document.querySelector('.regret');
  ret.addEventListener('click', regretqi);
  //悔棋功能-----结束



  //求和-----开始
  var qiuhe = document.querySelector('.qiuhe');
  qiuhe.addEventListener('click', function () {
    cover.style.zIndex = 1000;
    alert('比赛暂停啦！！');
    window.clearInterval(timerr);
  })
  //求和-----结束



  //认输-----开始
  var renshu = document.querySelector('.renshu');
  renshu.addEventListener('click', function () {
    cover.style.zIndex = 1000;
    window.clearInterval(timerr);
    if (flag == 0) {
      alert('白棋认输,黑棋获胜！！'); 
    } else {
      alert('黑棋认输,白棋获胜！！');
    }
  })
  //认输-----结束























}



/*
  胜利算法设计
  position
  1.判断有没有+1，+2，+3，+4
            -1,+1,+2,+3
            -2,-1,+1,+2
            -3,-2,-1,+1
            -4,-3,-2,-1
  2.        *16
  3.        *17
  4.        *-15
*/


























